<script setup lang="ts">
import { loginByPassword } from '@/services/user'
import { useUserStore } from '@/stores'
import { mobileRules, passwordRules } from '@/utils/rules'
import { showNotify, showToast } from 'vant'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const mobile = ref('13230000001')
const password = ref('abc12345')

// 明文密文切换
const isShowPassword = ref(true) //默认密文


// 获取仓库
const userStore = useUserStore()
// 获取路由实例
const router = useRouter()

// vue2如何使用   this.$router.push   this.$route 获取参数
// vue3如何使用   useRouter()   useRoute()

const login = async () => {
  // 自带整体校验  必须通过校验 才会触发
  if(!isAgree.value){
    showNotify('请先阅读并同意协议')
    return
  }
  // console.log('请求登录')
  const res = await loginByPassword(mobile.value, password.value)
  userStore.setUser(res.data) // 存储用户数据

  showToast('登录成功') // 提示

  // 跳转
  // router 除了可以导入 还可以调用一个 useRouter函数
  router.push('/user')

}


const isAgree = ref(true)



// const count: number = 2
// const money: typeof count = 100

// console.log(money, count)
</script>

<template>
  <div class="login-page">
    <!-- <van-tag type="primary">标签</van-tag>
    <van-tag type="success">标签</van-tag>
    <van-tag type="danger">标签</van-tag>
    <van-tag type="warning">标签</van-tag>
    <van-tag type="warning" text-color="#8df">嘿嘿</van-tag> -->
    <!-- <van-tag text-color=""></van-tag> -->
    <!-- 测试svg使用 -->
    <!--
      aria-hidden="true"
      这个属性表示在屏幕阅读器中，这个图标应该被忽略。

    -->
    <!-- <svg aria-hidden="true">
      <use href="#icon-home-docs"></use>
    </svg>
    <svg aria-hidden="true">
      <use href="#icon-home-graphic"></use>
    </svg>
    <svg aria-hidden="true">
      <use href="#icon-login-eye-off"></use>
    </svg>
    <CpIcon name="home-docs"></CpIcon>
    <CpIcon name="home-graphic"></CpIcon>
    <CpIcon name="login-eye-off"></CpIcon> -->
    <!-- 自己封装的组件没有提示 希望有 -->
    <!-- 自己的组件必须有个类型 -->
    <!-- <cp-icon name="home-docs"></cp-icon> -->
    <!-- <CpIcon></CpIcon> -->
    <!-- <CpIcon name="home-docs"></CpIcon> -->


    <!-- 头部 -->
    <div class="login-head">
      <h3>密码登录</h3>
      <a href="javascript:;">
        <span>短信验证码登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <!-- 点击登录按钮时，会触发submit事件，此时会调用login方法 -->
    <van-form autocomplete="off" @submit="login">
      <van-field :rules="mobileRules" v-model="mobile" placeholder="请输入手机号" type="tel"></van-field>
      <van-field
        :rules="passwordRules"
        v-model="password"
        placeholder="请输入密码"
        :type="isShowPassword?'password':'text'"
      >
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <!-- 但是需要添加一个属性  native-type="submit" -->
        <van-button native-type="submit" block round type="primary">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22,194,163,0.5);
    }
  }
}
</style>
